﻿using BaoVeThanhDia.Buttons;
using BaoVeThanhDia.Sound;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using NapThe;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Windows;


namespace BaoVeThanhDia.MapAndUpgrade
{
    class UpgradeInterface
    {
        List<Button> buttonupgrades; //các button
        Texture2D ImageGroup;
        List<UpgradeItem> items;
        SpriteFont font, bigfont;
        public static int Gold;
        UpgradeItem ItemSelected;
        CustomSoundEffect click, upgrade;
        IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
        bool isCheckedNapXu;
        public UpgradeInterface()
        {
            isCheckedNapXu = false;
            click = new CustomSoundEffect("sounds/effect/Click_Button");
            upgrade = new CustomSoundEffect("sounds/effect/UpgradeShop");
            buttonupgrades = new List<Button>();
            buttonupgrades.Add(new Button("", "images/mapandupgrade/bar", new Vector2(0, 420), new Vector2(0, 0), new Vector2(200, 15), 4));
            buttonupgrades.Add(new Button("", "images/mapandupgrade/bar", new Vector2(0, 0), new Vector2(0, 0), new Vector2(200, 15), 4));
            buttonupgrades.Add(new Button("", "images/upgradeSkill/CountGold", new Vector2(10, 422), new Vector2(0, 0), new Vector2(415, 107), 0.55f));
            buttonupgrades.Add(new Button("", "images/upgradeSkill/CountStar", new Vector2(400, 420), new Vector2(0, 0), new Vector2(249, 112), 0.6f));
            buttonupgrades.Add(new Button("", "images/upgradeSkill/noti", new Vector2(513, 70), new Vector2(0, 0), new Vector2(142, 175), 2));
            buttonupgrades.Add(new Button("trolai", "images/Introduction/trolai", new Vector2(650, 0), new Vector2(0, 0), new Vector2(228, 100), 0.6f));
            buttonupgrades.Add(new Button("nangcap", "images/upgradeSkill/up", new Vector2(600, 350), new Vector2(0, 0), new Vector2(239, 105), 0.5f));
            buttonupgrades.Add(new Button("muangoc", "images/upgradeSkill/muangoc", new Vector2(200, 420), new Vector2(0, 0), new Vector2(90, 80), 0.75f));
            items = new List<UpgradeItem>();
            for (int i = 0; i < 24; i++)
            {
                items.Add(new UpgradeItem(i, File_Upgrade_Star.ItemBought[i]));
            }
            ItemSelected = new UpgradeItem(0, File_Upgrade_Star.ItemBought[0]);
            Gold = NapTheDienThoaiController.numberOfMoney(storage);
        }
        public void Load(ContentManager contentManager)
        {
            click.Load(contentManager);
            upgrade.Load(contentManager);
            foreach (Button button in buttonupgrades)
            {
                button.Load(contentManager);
            }
            ImageGroup = contentManager.Load<Texture2D>("images/upgradeSkill/brd0x");
            foreach (UpgradeItem item in items)
            {
                item.Load(contentManager);
            }
            font = contentManager.Load<SpriteFont>("fonts/IntroFont");
            bigfont = contentManager.Load<SpriteFont>("fonts/XuFont");
        }
        public void Draw(ContentManager content, SpriteBatch spriteBatch, Vector2 Dolech, NapXu napxu)
        {
            if (!isCheckedNapXu && !napxu.isVisible())
            {
                isCheckedNapXu = true;
                Gold = NapTheDienThoaiController.numberOfMoney(storage);
            }
            ItemSelected.setBought(true);
            ItemSelected.Load(content);
            ItemSelected.setTouch(true);
            for (int i = 0; i < 6; i++)
            {
                spriteBatch.Draw(ImageGroup, new Vector2(10, 80 + 150 * i) + Dolech, new Rectangle(0, 0, 497, 114), Color.White, 0f, Vector2.Zero, 1, SpriteEffects.None, 0f);
            }
            spriteBatch.DrawString(font, "Kỹ năng vật lý", new Vector2(30, 85) + Dolech, Color.Black, 0, Vector2.Zero, 1f, SpriteEffects.None, 0);
            spriteBatch.DrawString(font, "Kỹ năng phép thuật", new Vector2(30, 235) + Dolech, Color.Black, 0, Vector2.Zero, 1f, SpriteEffects.None, 0);
            spriteBatch.DrawString(font, "Kỹ năng tháp cơ bản", new Vector2(30, 385) + Dolech, Color.Black, 0, Vector2.Zero, 1f, SpriteEffects.None, 0);
            spriteBatch.DrawString(font, "Kỹ năng tháp thần tiễn", new Vector2(30, 535) + Dolech, Color.Black, 0, Vector2.Zero, 1f, SpriteEffects.None, 0);
            spriteBatch.DrawString(font, "Kỹ năng tháp đại bác", new Vector2(30, 685) + Dolech, Color.Black, 0, Vector2.Zero, 1f, SpriteEffects.None, 0);
            spriteBatch.DrawString(font, "Kỹ năng tháp ma thuật", new Vector2(30, 835) + Dolech, Color.Black, 0, Vector2.Zero, 1f, SpriteEffects.None, 0);
            foreach (UpgradeItem item in items)
            {
                item.Draw(spriteBatch, Dolech, font);
            }
            foreach (Button button in buttonupgrades)
            {
                if (button.getName().Equals("nangcap"))
                {
                    if (checkUpgrade())
                        button.Draw(spriteBatch, new Vector2(0, 0));
                }
                else
                {
                    button.Draw(spriteBatch, new Vector2(0, 0));
                }
            }
            ItemSelected.OptionDraw(spriteBatch, new Vector2(610, 100));
            ItemSelected.DrawScript(spriteBatch, font);
            spriteBatch.DrawString(bigfont, "Nâng cấp", new Vector2(270, 6), Color.Black, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            spriteBatch.DrawString(bigfont, Gold.ToString(), new Vector2(80, 430), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            spriteBatch.DrawString(bigfont, File_Upgrade_Star.Star.ToString(), new Vector2(470, 430), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
        }
        public bool isTouch(Vector2 curtouch, Vector2 Dolech, ContentManager content, NapXu napxu) //  xử lí ng dùng
        {
            if (!napxu.isVisible())
            {
                #region kiểm tra các nút bấm
                foreach (Button button in buttonupgrades)
                {
                    if (!button.getName().Equals(""))
                    {
                        if (button.isTouch(curtouch))
                        {
                            button.setTouch(false);
                            if (button.getName().Equals("trolai"))
                            {
                                click.Play();
                                ItemSelected = new UpgradeItem(0, File_Upgrade_Star.ItemBought[0]);
                                return true;
                            }
                            else if (button.getName().Equals("muangoc"))
                            {
                                click.Play();
                                napxu.NapXuSms();
                                isCheckedNapXu = false;
                            }
                            else if (button.getName().Equals("nangcap") && checkUpgrade())
                            {
                                if (File_Upgrade_Star.Star >= ItemSelected.getPriceStar())
                                {
                                    upgrade.Play();
                                    items[ItemSelected.getIndex()].setBought(true);
                                    items[ItemSelected.getIndex()].Load(content);
                                    File_Upgrade_Star.Star -= ItemSelected.getPriceStar();
                                    File_Upgrade_Star.SaveItemBought(ItemSelected.getIndex(), true);
                                }
                                else
                                {
                                    MessageBoxResult result = MessageBox.Show("Bạn không có đủ sao để nâng cấp, bạn có muốn nâng cấp bằng ngọc không", "Thông báo", MessageBoxButton.OKCancel);
                                    if (result == MessageBoxResult.OK)
                                    {
                                        if (!napxu.CheckMoney(ItemSelected.getPriceStar() * 1000) && Gold >= ItemSelected.getPriceStar() * 1000)
                                        {
                                            Gold -= ItemSelected.getPriceStar() * 1000;
                                            napxu.SubMoney(ItemSelected.getPriceStar() * 1000);
                                            upgrade.Play();
                                            items[ItemSelected.getIndex()].setBought(true);
                                            items[ItemSelected.getIndex()].Load(content);
                                            File_Upgrade_Star.SaveItemBought(ItemSelected.getIndex(), true);
                                        }
                                        else
                                        {
                                            isCheckedNapXu = false;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                #endregion
                if (curtouch.Y < 420)
                {
                    #region kiểm tra các item
                    foreach (UpgradeItem item in items)
                    {
                        if (item.isTouch(curtouch - Dolech))
                        {
                            click.Play();
                            ItemSelected = new UpgradeItem(item.getIndex(), item.getisBought());
                        }
                    }
                    #endregion
                }
            }
            return false;
        }
        public void isTouch(Vector2 curtouch, Vector2 Dolech) //  check thường
        {
            foreach (Button button in buttonupgrades)
            {
                if (!button.getName().Equals(""))
                {
                    button.isTouch(curtouch);
                }
            }
            if (curtouch.Y < 420)
            {
                foreach (UpgradeItem item in items)
                {
                    item.isTouch(curtouch - Dolech);
                }
            }
        }
        public bool checkUpgrade()
        {
            int index = ItemSelected.getIndex();
            if (items[index].getisBought())
            {
                return false;
            }
            else
            {
                if (index % 4 == 0) return true;
                else
                {
                    if (items[index - 1].getisBought()) return true;
                    else return false;
                }
            }
        }
    }
}
